package com.azt.provider.impl;

import com.azt.api.pojo.Partner;
import com.azt.api.pojo.req.PartnerReq;
import com.azt.api.service.MsgConfigService;
import com.azt.api.service.PartnerService;
import com.azt.model.page.Pagination;
import com.azt.provider.mapper.PartnerMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PartnerServiceImpl implements PartnerService {

    @Autowired
	private PartnerMapper partnerMapper;

    @Autowired
    private MsgConfigService msgConfigService;

	@Override
	public Pagination partnerList(int pageno, int pagesize, Map<String, String> params) {
		Page<Object> startPage = PageHelper.startPage(pageno, pagesize);
		PageHelper.orderBy("createtime desc");
		List<Partner> partnerList =partnerMapper.selectAll();
		Pagination p = new Pagination(startPage, partnerList);
		p.setList(partnerList);
		return p;
	}

	@Override
	public void update(Partner partner) {
		 partnerMapper.updateByPrimaryKeySelective(partner);
	}

	@Override
	public void updateAuditOpinion(Integer id, String auditOpinion) {
		Partner partner = new Partner();
		partner.setId(id);
		partner.setAuditState(1);
		partner.setAuditOpinion(auditOpinion);
		partnerMapper.updateByPrimaryKeySelective(partner);
	}

	@Override
	public void save(PartnerReq req) {
		Partner partner = new Partner();
		BeanUtils.copyProperties(req, partner);
		partner.setAuditState(0);
		partnerMapper.insert(partner);
		// 发送短信通知运营
		Map<String, String> params = new HashMap<>();
		params.put("companyName", StringUtils.defaultString(partner.getCompanyName()));
		params.put("realName", partner.getRealName());
		params.put("mobile", partner.getMobile());
		params.put("type", new Integer(1).equals(partner.getType())? "城市合伙人": "平台合伙人");
		msgConfigService.sendMessage("PARTNER_APPLY", params);
	}
}
	


